package com.jxd.petMgr.service.impl;

import com.jxd.petMgr.dao.IPetStoreDao;
import com.jxd.petMgr.dao.impl.*;
import com.jxd.petMgr.model.Pet;
import com.jxd.petMgr.model.PetStore;
import com.jxd.petMgr.service.IPetStoreService;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

/**
 * @author lzh
 * @version 1.0
 * @Description
 * @date 2021/11/4
 */


public class PetStoreServiceImpl implements IPetStoreService {
    IPetStoreDao petStoreDao = new PetStoreDaoImpl();




    /**
     * 获得所有宠物商店对象
     *
     * @return 商店对象集合
     */
    @Override
    public List<PetStore> getAllStore() {
        return petStoreDao.getAllStore();
    }

    /**
     * 商店卖出宠物
     *更新商店表
     * @param pet 宠物
     * @return 是否卖出成功
     */
    @Override
    public boolean sell(Pet pet) {
        boolean result = false;//是否卖出成功
        BaseDao baseDao = new BaseDao();
        Connection conn = baseDao.getConn();
        PreparedStatement pstm = null;
        //更新商店表的余额
        PetStore petStore = petStoreDao.getPetStoreByID(pet.getStore_id());
        petStore.setBalance(petStore.getBalance() + pet.getPrice());
        try {
            result = petStoreDao.updatePetStore(petStore,conn,pstm);
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            baseDao.close(conn,pstm);
        }
        return result;
    }

    /**
     * 商店卖出宠物
     *
     * @param pets 宠物集合
     * @return 是否卖出成功
     */
    @Override
    public boolean sellBatch(List<Pet> pets) {
        boolean result = false;//是否卖出成功
        BaseDao baseDao = new BaseDao();
        Connection conn = baseDao.getConn();
        PreparedStatement pstm = null;
        //更新商店表的余额


        try {
            conn.setAutoCommit(false);//关闭自动提交
            for (Pet pet:pets) {
                PetStore petStore = petStoreDao.getPetStoreByID(pet.getStore_id());
                petStore.setBalance(petStore.getBalance() + pet.getPrice());
                petStoreDao.updatePetStore(petStore,conn,pstm);
            }
            conn.commit();
            result = true;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            baseDao.close(conn,pstm);
        }
        return result;
    }

    /**
     * 商店买宠物
     *
     * @param pet 宠物
     * @return 是否买入成功
     */
    @Override
    public boolean buy(Pet pet) {
        boolean result = false;//是否买入成功
        BaseDao baseDao = new BaseDao();
        Connection conn = baseDao.getConn();
        PreparedStatement pstm = null;
        //更新商店表的余额
        PetStore petStore = petStoreDao.getPetStoreByID(pet.getStore_id());
        petStore.setBalance(petStore.getBalance() - pet.getPrice());
        try {
            result = petStoreDao.updatePetStore(petStore,conn,pstm) ;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            baseDao.close(conn,pstm);
        }
        return result;
    }

    /**
     * 商店买宠物
     *
     * @param pets 宠物
     * @return 是否买入成功
     */
    @Override
    public boolean buyBatch(List<Pet> pets) {
        boolean result = false;//是否买入成功
        BaseDao baseDao = new BaseDao();
        Connection conn = baseDao.getConn();
        PreparedStatement pstm = null;


        try {
            conn.setAutoCommit(false);
            for (Pet pet:pets) {
                //更新商店表的余额
                PetStore petStore = petStoreDao.getPetStoreByID(pet.getStore_id());
                petStore.setBalance(petStore.getBalance() - pet.getPrice());
                result = petStoreDao.updatePetStore(petStore,conn,pstm) ;
            }
            conn.commit();
            result = true;
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                conn.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally {
            baseDao.close(conn,pstm);
        }
        return result;
    }


}
